package com.daotingli.backendprograms.Repository.Admin.Authoer;

import com.daotingli.backendprograms.Bean.Bean.t_userBean;
import com.daotingli.backendprograms.DB.SpringDB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import  java.util.List;
import java.util.Map;

@Repository
public class AuthoerRepository {
    @Autowired
    private SpringDB springDB;
    @Autowired
    private t_userBean userBean;

    public List<t_userBean> getAllAuthoers() {
        String  sql = "select * from t_user where identity = 2";
        List<t_userBean> list = new ArrayList<>();
        try{
            springDB.getJdbcTemplate().query(sql,(rs,rowNum)->{
                userBean = new t_userBean(rs.getInt("id"),rs.getString("username"),rs.getString("password"),rs.getInt("identity"),rs.getString("userphone"),rs.getInt("userstate"));
                list.add(userBean);
                return userBean;
            });

        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return list;
    }

    public Map<String, Object> addAuthoer(String username, String password, String identity, String userphone, String userstate) {
        String sql="insert into t_user(username,password,identity,userphone,userstate) values(?,?,?,?,?)";
        Map<String, Object> map = new HashMap<>();
        try{
            springDB.getJdbcTemplate().update(sql,username,password,identity,userphone,userstate);
            map.put("status", "success");
            map.put("message", "作者添加成功");

        }catch (Exception e){
            map.put("status", "error");
            map.put("message", "作者添加失败: " + e.getMessage());

        }
        return map;
    }

    public Map<String, Object> deleteAuthoer(Integer id) {
        String sql = "update t_user set userstate = 1 where id = ?";
        Map<String, Object> map = new HashMap<>();
        try{
            springDB.getJdbcTemplate().update(sql,id);
            map.put("status", "success");
            map.put("message", "作者删除成功");

        }catch (Exception e){
            map.put("status", "error");
            map.put("message", "作者删除失败: " + e.getMessage());

        }
        return map;
    }

    public Map<String, Object> updateAuthoer(Integer id, String username, String password, String identity, String userphone, String userstate) {
        String sql = "update t_user set username = ?,password = ?,identity = ?,userphone = ?,userstate = ? where id = ?";
        Map<String, Object> map = new HashMap<>();
        try{
            springDB.getJdbcTemplate().update(sql,username,password,identity,userphone,userstate,id);
            map.put("status", "success");
            map.put("message", "作者更新成功");

        }catch (Exception e){
            map.put("status", "error");
            map.put("message", "作者更新失败: " + e.getMessage());

        }
        return map;
    }
}
